package org.example.myleet.rosalind.utils;

import java.util.HashMap;
import java.util.Map;

public class RnaCodonReader {

    //https://rosalind.info/glossary/rna-codon-table/
    private static final String RNA_CODON = "" +
            "UUU F      CUU L      AUU I      GUU V\n" +
            "UUC F      CUC L      AUC I      GUC V\n" +
            "UUA L      CUA L      AUA I      GUA V\n" +
            "UUG L      CUG L      AUG M      GUG V\n" +
            "UCU S      CCU P      ACU T      GCU A\n" +
            "UCC S      CCC P      ACC T      GCC A\n" +
            "UCA S      CCA P      ACA T      GCA A\n" +
            "UCG S      CCG P      ACG T      GCG A\n" +
            "UAU Y      CAU H      AAU N      GAU D\n" +
            "UAC Y      CAC H      AAC N      GAC D\n" +
            "UAA Stop   CAA Q      AAA K      GAA E\n" +
            "UAG Stop   CAG Q      AAG K      GAG E\n" +
            "UGU C      CGU R      AGU S      GGU G\n" +
            "UGC C      CGC R      AGC S      GGC G\n" +
            "UGA Stop   CGA R      AGA R      GGA G\n" +
            "UGG W      CGG R      AGG R      GGG G\n";

    public static Map<String, String> getRnaCodonMapAminoAcid() {
        //初始化密码子表
        Map<String, String> codonMapAminoAcid = new HashMap<>(64);
        StringBuilder codon = new StringBuilder(4);
        StringBuilder aminoAcid = new StringBuilder(4);
        outer:
        for (int i = 0; i < RNA_CODON.length(); ++i) {
            char c = RNA_CODON.charAt(i);
            if (c == '\n') {
                continue;
            }
            i++;
            while (c != ' ') {
                codon.append(c);
                if (i >= RNA_CODON.length()) {
                    break outer;
                }
                c = RNA_CODON.charAt(i++);
            }
            if (i >= RNA_CODON.length()) {
                break;
            }
            c = RNA_CODON.charAt(i);
            while (c != ' ' && c != '\n') {
                aminoAcid.append(c);
                ++i;
                if (i >= RNA_CODON.length()) {
                    break outer;
                }
                c = RNA_CODON.charAt(i);
            }
            codonMapAminoAcid.put(codon.toString(), aminoAcid.toString());
            codon.delete(0, codon.length());
            aminoAcid.delete(0, aminoAcid.length());
            while (c == ' ') {
                ++i;
                if (i >= RNA_CODON.length()) {
                    break outer;
                }
                c = RNA_CODON.charAt(i);
            }
            --i;
        }
        return codonMapAminoAcid;
    }
}
